package com.example.carrierapp.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import java.lang.reflect.Method;

public class DisplayUtil {

    private static Typeface mTypeFace;

    public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }
        //获取屏幕的高度
    public static int getScreenHeight(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }
    /**
     * 获取状态栏高度
     * @return
     */
    public static int getStatusBarHeight(Context context){
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    //判断是否有smartbar
    public static boolean hasSmartBar(){
        try {
            Method method = Class.forName("android.os.Build").getMethod("hasSmartBar");
            return ((Boolean)method.invoke(null)).booleanValue();
        }catch (Exception e){
            if(Build.DEVICE.equals("mx2")){
                return true;
            }else if(Build.DEVICE.equals("mx")|| Build.DEVICE.equals("m9")){
                return false;
            }
            return false;
        }
    }

    public static int getSmartBarHeight(Context context){
        return hasSmartBar()==true?DisplayUtil.px2dip(context,48):0;
    }

    /**
     * 给textview 设置宋体
     * @param context
     * @param tv
     */
    public static void setSongTypeFace(Context context, TextView tv)
    {
        if(mTypeFace==null)
        {
            mTypeFace = Typeface.createFromAsset(context.getAssets(),"fonts/font_song.TTF");
        }
        tv.setTypeface(mTypeFace);
    }

    //处理全屏
    public static void hideNavKey(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //       设置屏幕始终在前面，不然点击鼠标，重新出现虚拟按键
            ((Activity) context).getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav
                            // bar
                            | View.SYSTEM_UI_FLAG_IMMERSIVE);
        } else {
            ((Activity) context).getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav
            );
        }
    }

    public static void setTextOrError(TextView tv,String text,String defaultString){
        tv.setText(TextUtils.isEmpty(text)?defaultString:text);
    }
}
